# Contributor: Tom Lebreux <me@tomlebreux.com>
# Maintainer: Tom Lebreux <me@tomlebreux.com>
pkgname=navidrome
pkgver=0.53.3
pkgrel=0
pkgdesc="Modern Music Server and Streamer compatible with Subsonic/Airsonic"
url="https://navidrome.org/"
arch="all !riscv64"
license="GPL-3.0-or-later"
# check: TypeError: Cannot redefine property: localStorage
# For downloading go and nodejs dependencies
options="net !check"
depends="ffmpeg"
makedepends="go nodejs npm sqlite-dev taglib-dev zlib-dev"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
pkgusers="navidrome"
pkggroups="navidrome"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/navidrome/navidrome/archive/refs/tags/v$pkgver.tar.gz
	navidrome.initd
	navidrome.confd
	navidrome.toml
	"

export GOFLAGS="$GOFLAGS -tags=embed,netgo,libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# secfixes:
#   0.47.5-r0:
#     - CVE-2022-23857

prepare() {
	default_prepare

	go mod download
	go mod tidy

	cd ui
	# Prevents crash due to allocation failure
	echo 'node-options=--max_old_space_size=3072' >.npmrc
	npm install
	cd ..
}

build() {
	cd ./ui/
	npm run build
	cd ..

	# Display pkgrel instead of keeping the git sha in sync with pkgver
	CGO_ENABLED=1 go build \
		-ldflags="
			-X github.com/navidrome/navidrome/consts.gitSha=$pkgver-r$pkgrel
			-X github.com/navidrome/navidrome/consts.gitTag=v$pkgver"
}

check() {
	make testall
}

package() {
	install -Dm755 navidrome "$pkgdir"/usr/bin/navidrome

	install -Dm644 "$srcdir"/navidrome.toml \
		"$pkgdir"/etc/navidrome/navidrome.toml

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname

	install -dm755 -o navidrome -g navidrome \
		"$pkgdir"/var/lib/navidrome \
		"$pkgdir"/var/lib/navidrome/music
}

sha512sums="
58c4b91471175728184afe0ce345a77042277a70ce3ce6a67a5b8e8b11f31fee3938ec8c5b0fa20037b34e3e9a9162164fafd578476265a09e83f10acfb08e2f  navidrome-0.53.3.tar.gz
9622f9e30b8964e7e2708d313f45e0977fdfa4ceec9b4311f8a40bd250c986fcc87f24577e74ccbcf06d4f3d071f0951d5e25eb3107ce0188b7caf6fe073edaa  navidrome.initd
dc5a0f21c5da6c165c4dc216b8dc3cab8e2c02c5c2ff06dc393528383234034428df7cfa11704a2373fcebba4cba484fffd2231b3c5fd871420a34fe1dcf1e04  navidrome.confd
0a9cdc6f1f57bb44a683f7c810c290cbdd9b0d618b37da82b722dade6203c7e692c9058e044f797d4f29254588d1b086813daec6f5fba88893d998d2092d0d38  navidrome.toml
"
